<template>
	<view>
		<u-popup :show="screen_show" @close="close" @open="open" mode="top">
			<view>
				<view class="screen_box">
					<view class="screen_option">
						<text :class="team_index == ''?'on':''" @click="_team(0)">全部</text>
						<text :class="team_index == 1?'on':''" @click="_team(1)">直营</text>
						<text :class="team_index == 2?'on':''" @click="_team(2)">团队</text>
					</view>
					<text>支付方式</text>
					<view class="screen_option">
						<text :class="on_index == ''?'on':''" @click="_tab(0)">全部</text>
						<text :class="on_index == 1?'on':''" @click="_tab(1)">交易分润</text>
						<text :class="on_index == 2?'on':''" @click="_tab(2)">流量卡</text>
						<text :class="on_index == 13?'on':''" @click="_tab(13)">D0笔数费</text>
					</view>
					<text>交易日期区间</text>
					<view class="screen_option">
						<u-datetime-picker style="flex: 0;" :show="start" v-model="start_value" mode="date"
							:min-date="minday" :max-date="maxday" @confirm="confirm($event,1)" closeOnClickOverlay
							@cancel="cancel(1)"></u-datetime-picker>
						<text class="datetime" @click="start = true">{{start_text}}</text>
						<view style="margin: 0 30rpx;">至</view>
						<u-datetime-picker style="flex: 0;" :show="end" v-model="end_value" mode="date"
							:min-date="minday" :max-date="maxday" @confirm="confirm($event,2)" closeOnClickOverlay
							@cancel="cancel(2)">
						</u-datetime-picker>
						<text class="datetime" @click="end = true">{{end_text}}</text>
					</view>
				</view>
				<view class="btn">
					<text @click="btn_confirm">确认</text>
				</view>
			</view>
		</u-popup>
		<u-sticky bg-color="#fff">
			<view class="balance">
				<view class="balance_top">
					<text>{{text_data}}</text>
					<text class="blues" @click="screen_show = true">筛选</text>
				</view>
			</view>
		</u-sticky>
		<view class="account_conts">
			<text>合计：</text>
			<text>{{money_total}}</text>
		</view>
		<view class="account_box">
			<view class="account_item" v-if="display">
				<view class="account_list" v-for="(item,index) in data" :key="index">
					<view class="account_cont">
						<image v-if="item.balance_type_id == 1" src="@/static/fenrun.png" mode=""></image>
						<image v-if="item.balance_type_id == 4" src="@/static/dabiao.png" mode=""></image>
						<image v-if="item.balance_type_id == 3" src="@/static/liuliangka.png" mode=""></image>
						<image v-if="item.balance_type_id == 2" src="@/static/fanxian.png" mode=""></image>
						<image v-if="item.balance_type_id == 6" src="@/static/deposit.png" mode=""></image>
						<image v-if="item.balance_type_id == 13" src="@/static/bishu.png" mode=""></image>
						<view class="" style="width: 100%;">
							<view class="increase">
								<view class="">
									<text>{{item.mome}}</text>
									<text v-if="item.team_type == 1" class="teams green-txt">{{item.subtitle}}</text>
									<text v-else class="teams red-txt">{{item.subtitle}}</text>
								</view>
								<text class="green-txt">+{{item.changes}}</text>
							</view>
							<view class="item-text">SN：{{item.sn}}</view>
							<view class="item-text" v-if="item.balance_type_id != 2">金额：{{item.trades.money}}</view>
							<view class="item-text" v-else>次数：第{{item.trades.number}}次</view>
							<view class="item-text" v-if="item.balance_type_id == 1">交易类型：{{item.trades.order_type_txt}}
							</view>

							<view class="item-text">时间：{{item.time}}</view>
						</view>
					</view>
				</view>
			</view>
			<u-empty v-else mode="list" icon="/static/Empty/data_null.png" text="您还没有收益呢">
			</u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tab: [],
				current: 0,
				data: [],
				page: 1,
				start: false,
				start_text: '选择开始日期',
				start_value: Number(new Date()),
				start_time: '',
				end: false,
				end_text: '选择结束日期',
				end_value: Number(new Date()),
				end_time: '',
				screen_show: false,
				on_index: '',
				bottom: true,
				display: false,
				search_type: false,
				text_data: this.maxday,
				money_total: '0.00',
				team_index: ''
			}
		},
		//上拉加载
		onReachBottom() {
			if (this.bottom == true) {
				this.page += 1
				this._request()
			}
		},
		onLoad(e) {
			uni.pageScrollTo({
				scrollTop: this.scrollTop
			});
			if (e.id == 1) {
				var date = new Date();
				let endDate = date.getDate();
				let _endDate = new Date(date.setDate(endDate));
				date.setDate(1);
				let beginDate = date.toISOString().slice(0, 10)
				this.start_time = beginDate
				this.end_time = _endDate.getFullYear() + "-" + (_endDate.getMonth() + 1) + "-" + _endDate.getDate()
				if (this.start_time != '' && this.end_time != '') {
					this.text_data = this.start_time + ' 至 ' + this.end_time
				}
				this._request()
			} else {
				let _date = new Date();
				// _date.setTime(_date.getTime() - 24 * 60 * 60 * 1000);
				_date.setTime(_date.getTime());
				this.text_data = _date.getFullYear() + "-" + (_date.getMonth() + 1) + "-" + _date.getDate()
				this._request()
			}

		},
		computed: {
			maxday() {
				let day = new Date();
				// day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
				day.setTime(day.getTime());
				let days = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate()
				let max_day = new Date(days).getTime()
				return max_day
			},
			minday() {
				let day = new Date(); // 获取今天的日期
				day.setDate(day.getDate() - 180) // 今天的前N天的日期，N自定义
				let days = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate()
				let min_day = new Date(days).getTime()
				return min_day
			}
		},
		methods: {
			_tab(type) {
				if (type == 0) {
					this.on_index = ''
				} else {
					this.on_index = type
				}
			},
			_team(type) {
				if (type == 0) {
					this.team_index = ''
				} else {
					this.team_index = type
				}
			},
			btn_confirm() {
				uni.pageScrollTo({
					scrollTop: this.scrollTop
				});
				this.screen_show = false
				this.search_type = true
				this.page = 1
				this.data = []
				this.bottom = true
				if (this.start_time != '' && this.end_time != '') {
					this.text_data = this.start_time + ' 至 ' + this.end_time
				}
				this._request()
			},
			open() {
				// console.log('open');
			},
			close() {
				this.screen_show = false
			},
			confirm(e, id) {
				const timeFormat = uni.$u.timeFormat
				switch (id) {
					case 1:
						//开始的时间
						this.start_text = timeFormat(e.value, 'yyyy-mm-dd')
						this.start_time = timeFormat(e.value, 'yyyy-mm-dd')
						this.start = false
						break
					case 2:
						//结束的时间
						this.end_text = timeFormat(e.value, 'yyyy-mm-dd')
						this.end_time = timeFormat(e.value, 'yyyy-mm-dd')
						this.end = false
						break
				}
			},
			cancel(id) {
				if (id == 1) {
					this.start = false
				} else if (id == 2) {
					this.end = false
				}
			},
			async _request() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'balance/getMoneyLogs',
					data: {
						balance_type_id: this.on_index,
						money_type: '',
						team_type: this.team_index,
						page: this.page,
						page_size: 15,
						start_time: this.start_time,
						end_time: this.end_time
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.money_total = res.data.money_total
					if (this.search_type) {
						if (res.data.data !== undefined && res.data.data !== null && res.data.data.length >
							0) {
							this.display = true
							if (this.page == 1) {
								this.data = res.data.data

							} else {
								this.data = [...this.data, ...res.data.data]
							}
						} else {
							this.bottom = false
							this.display = false
							if (this.page > 1) {
								this.display = true
							}
						}
					} else {
						if (res.data.data !== undefined && res.data.data !== null && res.data.data.length >
							0) {
							this.display = true
							if (this.page == 1) {
								this.data = res.data.data
							} else {
								this.data = [...this.data, ...res.data.data]
							}
						} else {
							this.bottom = false
							this.display = false
							if (this.page > 1) {
								this.display = true
							}
						}
					}
				} else {
					uni.showToast({
						title: res.msg,
						duration: 1000,
						icon: "none"
					});
					this.bottom = false
					this.display = false
				}
			},
			info() {
				uni.navigateTo({
					url: '/pages/mine/account/account_info?id=' + this.tab[this.current].balance_type_id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F2F6FC;
	}

	.blues {
		color: #2979ff !important;
	}

	.screen_box {
		padding: 30rpx 50rpx;
		box-sizing: border-box;

		.screen_option {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			// justify-content: space-between;
			margin: 30rpx 0;

			text {
				background-color: #eee;
				border: #eee 1px solid;
				padding: 10rpx 20rpx;
				color: #333;
				border-radius: 15rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 0 5rpx 30rpx 5rpx;
			}

			text.on {
				background-color: #eee;
				border: #55aaff 1px solid;
				padding: 10rpx 20rpx;
				color: #55aaff;
				border-radius: 15rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.datetime {
				margin: 0;
			}
		}
	}

	.btn {
		display: flex;
		align-items: center;
		justify-content: center;

		text {
			width: 100%;
			padding: 30rpx 0;
			text-align: center;
			background-color: #55aaff;
			color: #fff;
		}
	}

	.balance {
		display: flex;
		flex-direction: column-reverse;
		padding: 30rpx;
		box-sizing: border-box;
		margin-bottom: 30rpx;

		.balance_top {
			display: flex;
			justify-content: space-between;
			// flex-direction: column;
			// align-items: center;
			width: 100%;

			text {
				font-size: 28rpx;
				color: #666666;
			}

			h2 {
				font-weight: 500;
				margin-top: 10rpx;
				margin-bottom: 30rpx;
				font-size: 42rpx;
			}
		}


		.expenses {
			display: flex;
			margin-bottom: 30rpx;
			box-sizing: border-box;
			align-items: center;
			justify-content: center;

			.dbs {
				// width: 100%;
			}

			.expenses_time {
				display: flex;
				justify-content: flex-start;
				align-items: center;

				.order {
					color: #fff;
					font-size: 28rpx;
					padding: 5rpx 20rpx;
					background-color: #1e80ff;
					border-radius: 15rpx;
					margin-left: 50rpx;
				}
			}

			text {
				font-size: 24rpx;
				color: #999999;
			}
		}
	}

	.account_box {
		padding: 0 30rpx;

		.account_list {
			background-color: #fff;
			border-bottom: #f8f8f8 1px solid;
			border-radius: 15rpx;
			padding: 25rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 30rpx;

			.account_cont {
				display: flex;
				align-items: center;
				width: 100%;

				image {
					width: 60rpx;
					height: 55rpx;
					margin-right: 20rpx;
				}

				text {
					color: #999;
					font-size: 24rpx;
				}

				.item-text {
					font-size: 24rpx;
					color: #999;
					padding-top: 10rpx;
				}
			}

			.increase {
				display: flex;
				justify-content: space-between;
				font-size: 24rpx;
				width: 100%;

				text {
					color: #333;
					font-size: 28rpx;
				}

				.green-txt {
					color: #19be6b;
				}

				.red-txt {
					color: #fa3534;
				}

				.teams {
					padding-left: 20rpx;
				}
			}
		}

		.account_list:last-child {
			border-bottom: none;
		}
	}

	.account_conts {
		padding: 0 30rpx 30rpx 30rpx;
		color: #fa3534;
		font-size: 26rpx;
	}
</style>